//
//  JCLVisitorView.swift
//  WeiBo
//
//  Created by DDC on 2017/9/8.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit

class JCLVisitorView: UIView {

    
    ///注册按钮
    lazy var registerButton:UIButton = UIButton.hm_button(
        withTitle: "注册",
        fontSize: 16,
        textColor: UIColor.orange,
        higlightTextColor: UIColor.black,
        backgroudImage: "common_button_white_disable")
    ///登录按钮
    lazy var loginButton:UIButton = UIButton.hm_button(
        withTitle: "登录",
        fontSize: 16,
        textColor: UIColor.black,
        higlightTextColor: UIColor.darkGray,
        backgroudImage: "common_button_white_disable")
    
    var visitorInfo:[String:String]? {
        
        didSet {
            guard let imageName = visitorInfo?["imageName"],
                  let message = visitorInfo?["message"]   else {
                return
            }
            
            descriptionLabel.text = message
            //首页不需要设置imageName
            if imageName == "" {
                //首页界面 开始动画
                startAnimation()
                return
            }
            //其他界面需要更换imageName
            bgImgView.image = UIImage.init(named: imageName)
            foreImgView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    fileprivate func startAnimation() {
        let anim = CABasicAnimation(keyPath:"transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 10
        
        anim.isRemovedOnCompletion = false//完成之后不删除，一直动画
        bgImgView.layer.add(anim, forKey: nil)
    }
    
    //添加访客视图控件
    ///背景图片
    fileprivate lazy var bgImgView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

    ///前景图片
    fileprivate lazy var foreImgView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    ///描述文字
    fileprivate lazy var descriptionLabel:UILabel = UILabel.hm_label(
        withText: "没有朋友，就赶紧去注册一下吧! 没有朋友，就赶紧去注册一下吧！",
        fontSize: 14,
        textColor:
        UIColor.darkGray,
        alignment: .center)
    
    //遮罩视图
    fileprivate lazy var maskIconView:UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
    
    
}


extension JCLVisitorView
{
    func setupUI(){
        
        backgroundColor = UIColor.hm_color(withHex: 0xEDEDED)
        //添加控件
        addSubview(bgImgView)
        addSubview(maskIconView)
        addSubview(foreImgView)
        addSubview(descriptionLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //取消自动布局autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //添加布局到视图
        //bgImgView
        addConstraint(NSLayoutConstraint.init(item: bgImgView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: bgImgView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        
        //foreImgView
        addConstraint(NSLayoutConstraint.init(item: foreImgView, attribute: .centerY, relatedBy: .equal, toItem: bgImgView, attribute: .centerY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: foreImgView, attribute: .centerX, relatedBy: .equal, toItem: bgImgView, attribute: .centerX, multiplier: 1.0, constant: 0))
        
        //descriptionLabel
        addConstraint(NSLayoutConstraint.init(item: descriptionLabel, attribute: .centerX, relatedBy: .equal, toItem: foreImgView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: descriptionLabel, attribute: .top, relatedBy: .equal, toItem: bgImgView, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint.init(item: descriptionLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 240))
        
        //registerButton
        addConstraint(NSLayoutConstraint.init(item: registerButton, attribute: .left, relatedBy: .equal, toItem: descriptionLabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: registerButton, attribute: .top, relatedBy: .equal, toItem: descriptionLabel, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint.init(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        
        //loginButton
        addConstraint(NSLayoutConstraint.init(item: loginButton, attribute: .right, relatedBy: .equal, toItem: descriptionLabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: loginButton, attribute: .top, relatedBy: .equal, toItem: descriptionLabel, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint.init(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        
        //maskIconView
        addConstraint(NSLayoutConstraint.init(item: maskIconView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: maskIconView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 1.0, constant: -230))
        
    }
    
    
}
